﻿using Youz.Common.BLL;
using Youz.Common.Helper;
using Youz.Common.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Youz.CreditRepay.Manage.Handlers
{
    /// <summary>
    /// JSLanage 的摘要说明
    /// </summary>
    public class JSLanguage : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            string type = context.Request.QueryString["type"];
            if (string.IsNullOrEmpty(type))
            {
                context.Response.ContentType = "application/x-javascript";
                context.Response.Write(Build());
            }
            else if (type.ToLower() == "add")
            {
                context.Response.ContentType = "text/plain";
                Add();
            }
            else if (type.ToLower() == "change")
            {
                context.Response.ContentType = "text/plain";
                ChangeLanguage();
            }
            context.Response.End();
        }

        private string Build()
        {
            return Lang_LanguageBLL.GetJS();
        }

        private void Add()
        {
            string key = HttpContext.Current.Request.Form["Key"];
            string value = HttpContext.Current.Request.Form["Value"];

            Lang_LanguageBLL bll = new Lang_LanguageBLL();
            bll.AddBatch(new Lang_Language { ModuleName = Convert.ToString(LanguageModule.M_JS), WebSite = (int)Utility.CurrentSiteLanguageID, LanguageKey = key, LanguageValue = value });
        }

        /// <summary>
        /// 切换系统语言
        /// </summary>
        private string ChangeLanguage()
        {
            Utility.CurrentLanguage = WebHelper.Post<String>("LanguageKey");
            return Utility.AjaxResult(true);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}